package com.doyens.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;

@Component
@Slf4j
public class MymetaObjectHandler implements MetaObjectHandler {

    /**
     * 自定义类实现MetaObjectHandler类
     * 重写insertFill方法和updateFill方法
     * 自定义添加或更新时填充字段
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共字段自动填充[insert]");
        log.info(metaObject.toString());
        metaObject.setValue("createTime",LocalDateTime.now());
        metaObject.setValue("updateTime",LocalDateTime.now());
        //这里获取当前登录用户的id直接从BaseContext类中获取
//        metaObject.setValue("createUser",BaseContext.getCurrId());
//        metaObject.setValue("updateUser",BaseContext.getCurrId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段自动填充[update]");
        log.info(metaObject.toString());
        metaObject.setValue("updateTime",LocalDateTime.now());
//        metaObject.setValue("updateUser",BaseContext.getCurrId());
    }
}
